package com.forsir.android.mhfinance.storeObjects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.database.Cursor;

import com.forsir.android.mhfinance.helper.DbAdapter;

public class SubcategoryList {
	private List<Subcategory> mSubcategories = new ArrayList<Subcategory>();
	public boolean isEmpty = true;

	public Subcategory createNew = null;

	public SubcategoryList() {
	}

	public Subcategory getCreateNewSubcategory() {
		if (createNew == null) {
			createNew = new Subcategory();
			createNew.setId(-1);
			createNew.setName("Create new");
		}
		return createNew;
	}

	public void setSubcategories(List<Subcategory> subcategories) {
		mSubcategories = subcategories;
	}

	public List<Subcategory> getSubcategories() {
		return mSubcategories;
	}

	public void loadAll(DbAdapter dbHelper, long parentId) {
		final Cursor cursor = dbHelper.fetchAllEntries(new Subcategory(), DbAdapter.CATEGORY_PARENT + " = " + parentId, DbAdapter.CATEGORY_NAME);
		mSubcategories = new ArrayList<Subcategory>();

		isEmpty = false;
		if (!cursor.moveToFirst()) {
			cursor.close();
			return;
		}

		do {
			mSubcategories.add(new Subcategory(cursor));
		} while (cursor.moveToNext());
		cursor.close();
	}

	public void loadAllIfEmpty(DbAdapter dbHelper, long parentId) {
		if (isEmpty) {
			loadAll(dbHelper, parentId);
		}
	}

	public String[] getNames() {
		final int size = mSubcategories.size();
		final String[] retString = new String[size];
		for (int i = 0; i < size; i++) {
			retString[i] = mSubcategories.get(i).getName();
		}
		return retString;
	}

	public List<Map<String, String>> getMainGroup(Subcategory addedSubcategory, boolean onlyIfEmpty) {
		final List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		final int count = mSubcategories.size();

		for (int i = 0; i < count; i++) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			curGroupMap.put("id", String.valueOf(mSubcategories.get(i).getId()));
			curGroupMap.put("name", mSubcategories.get(i).getName());
			groupData.add(curGroupMap);
		}

		if (addedSubcategory != null && ((count == 0 && onlyIfEmpty) || (!onlyIfEmpty))) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			curGroupMap.put("id", "-1");
			curGroupMap.put("name", addedSubcategory.getName());
			groupData.add(curGroupMap);
		}

		return groupData;
	}

	public Subcategory getSubcategory(int position) {
		if (position >= mSubcategories.size()) {
			return null;
		}
		return mSubcategories.get(position);
	}

	public Subcategory getSubcategoryById(long categoryId) {
		final int count = mSubcategories.size();
		for (int i = 0; i < count; i++) {
			if (mSubcategories.get(i).getId() == categoryId) {
				return mSubcategories.get(i);
			}
		}

		return null;
	}

	public List<Map<String, String>> getChildGroup(String currencyBefore, String currencyAfter) {
		final List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		final int count = mSubcategories.size();
		Subcategory subcategory;

		for (int i = 0; i < count; i++) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			subcategory = mSubcategories.get(i);

			curGroupMap.put("id", String.valueOf(subcategory.getId()));
			curGroupMap.put("name", subcategory.getName());
			if (subcategory.count == 0) {
				curGroupMap.put("count", "");
			} else {
				curGroupMap.put("count", String.valueOf(subcategory.count) + "x");
			}
			curGroupMap.put("deposit", Payment.formatAmountString(currencyBefore, subcategory.summaryDeposit, currencyAfter));
			curGroupMap.put("withdraw", Payment.formatAmountString(currencyBefore, subcategory.summaryWithdraw, currencyAfter));
			curGroupMap.put("summary", Payment.formatAmountString(currencyBefore, subcategory.summaryDeposit + subcategory.summaryWithdraw, currencyAfter));
			groupData.add(curGroupMap);
		}

		return groupData;
	}

	public void fillChildSummary(HashMap<Long, String[]> data) {
		// data must be sorted: id, count, deposit, withdraw
		final int count = mSubcategories.size();
		Subcategory subcategory;
		String[] valuesList;
		for (int i = 0; i < count; i++) {
			if (data.containsKey(mSubcategories.get(i).getId())) {
				subcategory = mSubcategories.get(i);
				valuesList = data.get(subcategory.getId());
				subcategory.count += Integer.parseInt(valuesList[0]);
				subcategory.summaryDeposit += Float.parseFloat(valuesList[1]);
				subcategory.summaryWithdraw += Float.parseFloat(valuesList[2]);
			}
		}
	}

	public void cleanSummary() {
		final int count = mSubcategories.size();
		Subcategory subcategory;
		for (int i = 0; i < count; i++) {
			subcategory = mSubcategories.get(i);
			subcategory.summaryDeposit = 0;
			subcategory.summaryWithdraw = 0;
			subcategory.count = 0;
		}
	}

	public float getSumWithdraw() {
		final int count = mSubcategories.size();
		float sum = 0;
		for (int i = 0; i < count; i++) {
			sum += mSubcategories.get(i).summaryWithdraw;
		}
		return sum;
	}

	public float getSumDeposit() {
		final int count = mSubcategories.size();
		float sum = 0;
		for (int i = 0; i < count; i++) {
			sum += mSubcategories.get(i).summaryDeposit;
		}
		return sum;
	}

	public int getSumCount() {
		final int count = mSubcategories.size();
		int sum = 0;
		for (int i = 0; i < count; i++) {
			sum += mSubcategories.get(i).count;
		}
		return sum;
	}

}
